# -*- coding:utf-8 -*-
# @FileName  :adhcc_0_setup.py
# @Time      :2020/12/25 14:54
# @Author    :Zhoujirong
# @Desc      : 初始化角色和test用户

from common.log import Log
from biz.flow.role import RoleRequest
from biz.flow.user import UserRequest
from biz.cases.base_case import BaseCase
from common.utils import get_redis,get_mysql

class TestSetUp(BaseCase):
    '''初始化角色、第一个用户新建、关联角色、第一次登陆、修改密码'''
    data_file_name = 'setup.yaml'

    @classmethod
    def setUpClass(cls):
        cls.redis = get_redis()
        cls.cookie_admin = BaseCase().get_token_admin()

    def test_a_role_create(self):
        '''添加角色'''
        role_name = self.file_data.get('role_name')
        roleType = self.file_data.get('add_roleType')
        description = self.file_data.get('add_description')
        check = self.file_data.get('check_create')
        ret = RoleRequest.role_add(role_name,roleType,description,self.cookie_admin)
        self.assertEqual(check, ret.msg, '返回的的不是操作成功')
        Log.info(ret)

    def role_get_page(self):
        curPage = self.file_data.get('curPage')
        maxSize = self.file_data.get('maxSize')
        search_LIKE_name = self.file_data.get('role_name')
        search_EQ_roleType = self.file_data.get('add_roleType')
        ret = RoleRequest.role_getpage(curPage,maxSize,search_LIKE_name,search_EQ_roleType,self.cookie_admin)
        self.assertEqual(search_LIKE_name,ret.list[0].name)
        return ret.list[0].id

    def test_b_role_munu(self):
        '''角色关联菜单'''
        role_id = self.role_get_page()
        roleIdAndMenuId = '%s,%s'%(role_id,self.file_data.get('roleIdAndMenuId'))
        ret = RoleRequest.grantpower(roleIdAndMenuId,self.cookie_admin)
        Log.info(ret)
        self.assertEqual('00000000',ret.code)
        self.assertEqual('操作成功',ret.msg)

    def test_c_per_per(self):
        '''个人监管权限'''
        type = self.file_data.get('type')
        userId = self.file_data.get('userId')
        role_id = TestSetUp().role_get_page()
        ids = '%s,%s'%(role_id,self.file_data.get('ids'))
        ret = RoleRequest.per_saveuserreason(type,userId,ids,self.cookie_admin)
        Log.info(ret)
        self.assertEqual('00000000', ret.code)
        self.assertIn('操作成功', ret.msg)

    def test_d_ent_per(self):
        '''企业监管权限'''
        type = self.file_data.get('type_ent')
        userId = self.file_data.get('userId_ent')
        role_id = TestSetUp().role_get_page()
        ids = '%s,%s'%(role_id,self.file_data.get('ids_ent'))
        ret = RoleRequest.ent_saveuserreason(type,userId,ids,self.cookie_admin)
        Log.info(ret)
        self.assertEqual('00000000', ret.code)
        self.assertIn('操作成功', ret.msg)

    def test_e_create_user(self):
        '''用admin新建第一个用户'''
        userName = self.file_data.get('userName')
        nickName = self.file_data.get('nickName')
        idCard = self.file_data.get('idCard')
        orgId = self.file_data.get('orgId')
        userType = self.file_data.get('userType')
        telephone = self.file_data.get('telephone')
        mobile = self.file_data.get('mobile')
        weiChat = self.file_data.get('weiChat')
        userEmail = self.file_data.get('userEmail')
        orgPhone = self.file_data.get('orgPhone')
        postCode = self.file_data.get('postCode')
        address = self.file_data.get('address')
        deptName = self.file_data.get('deptName')
        ipbindflag = self.file_data.get('ipbindflag')
        ipbindnum = self.file_data.get('ipbindnum')
        macbindflag = self.file_data.get('macbindflag')
        macbindnum = self.file_data.get('macbindnum')
        mark = self.file_data.get('mark')
        lockFlag = self.file_data.get('lockFlag')
        resetFlag = self.file_data.get('resetFlag')
        stopFlag = self.file_data.get('stopFlag')
        roleIds = self.role_get_page()
        assignRoleIds = roleIds
        ret = UserRequest.create(userName,
                                 nickName,
                                 idCard,
                                 orgId,
                                 userType,
                                 telephone,
                                 mobile,
                                 weiChat,
                                 userEmail,
                                 orgPhone,
                                 postCode,
                                 address,
                                 deptName,
                                 ipbindflag,
                                 ipbindnum,
                                 macbindflag,
                                 macbindnum,
                                 mark,
                                 lockFlag,
                                 resetFlag,
                                 stopFlag,
                                 roleIds,
                                 assignRoleIds, self.cookie_admin)
        Log.info(ret)
        self.assertEqual('00000000', ret.code)
        self.assertEqual('操作成功', ret.msg)
        self.assertIsNotNone(ret.objectData)

    def login_first(self):
        '''用户test首次登录'''
        username = self.file_data.get('userName')
        password = self.file_data.get('pwd_origin')
        phonecode = self.file_data.get('phonecode')
        captcha = self.file_data.get('captcha')
        mode = self.file_data.get('mode')
        needPWD = self.file_data.get('needPWD')
        ret = UserRequest.login_cookie(username,password,phonecode,captcha,mode,needPWD)
        Log.info(ret)
        return ret

    def test_f_update_pwd(self):
        '''修改用户test的密码'''
        password = self.file_data.get('password')
        newpassword  = self.file_data.get('new')
        new = self.file_data.get('new')
        cookie = self.login_first()
        ret = UserRequest.updatepasswd(password,
                                        newpassword,
                                        new,cookie)
        Log.info(ret)
        self.assertEqual('00000000',ret.code)
        self.assertEqual('操作成功',ret.msg)






















if __name__ == "__main__":
    pass